<?php
/**
 * Created by PhpStorm.
 * User: root
 * Date: 15-1-10
 * Time: 下午5:18
 */
namespace Student;

class LessonStudentController extends \LessonStudentController
{
    public function index($lessonId)
    {
        $students = \Lesson_Student::with('student')
            ->with('status')
            ->where('lesson_id', $lessonId)
            ->where('status_id', '!=', 2)
            ->get()->toArray();

        return \Response::json($students);
    }

    public function store($lessonId)
    {
        $targetLesson = \Lesson::findOrFail($lessonId);

        $studentId = \Auth::user()['id'];

        $targetStudent = \User::where('id', $studentId)
            ->orWhere('username', $studentId)
            ->orWhere('email', $studentId)
            ->firstOrFail();

        $targetLesson->student()->save($targetStudent);

        return \Response::make('', 200);
    }

    public function destroy($lessonId, $studentId)
    {
        $studentId = \Auth::user()['id'];
        $targetPivot = \Lesson_Student::where('lesson_id', $lessonId)
            ->where('student_id', $studentId)
            ->firstOrFail();

        $targetPivot->update(['status_id' => 3]);

        return \Response::make('', 200);
    }
}